package com.example.liu_test.controller;


import com.example.liu_test.config.UserLoginToken;
import com.example.liu_test.mapper.UserMapper;
import com.example.liu_test.pojo.RespBean;
import com.example.liu_test.pojo.User;
import com.example.liu_test.service.IUserService;
import com.example.liu_test.service.UserVoService;
import com.example.liu_test.service.impl.TokenService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 刘旺
 * @since 2021-07-06 上午
 */
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private UserVoService userVoService;
    @Autowired
    private UserMapper userMapper;


    /**
     * 多表查询功能自定义sql
     *
     * @author: lw
     * @date 2021/07/06 下午
     */
    @UserLoginToken
    @ApiOperation(value = "查询", notes = "查询功能")
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public RespBean selectUser() {
        return userVoService.queryAll();
    }

    /**
     * 新增用户
     *
     * @author lw
     * @data 2021/07/07 上午
     */
    @UserLoginToken
    @ApiOperation(value = "新增", notes = "查询功能")
    @RequestMapping(value = "/addAll", method = RequestMethod.POST)
    public RespBean addUser(User user) {
        Integer integer = Integer.valueOf(user.getUserId());
        user.setUserId(integer);
        return iUserService.insertUser(user);
    }

    /**
     * 删除用户
     *
     * @author lw
     * @data 2021/07/06 下午
     */
    @UserLoginToken
    @ApiOperation(value = "删除", notes = "查询功能")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public RespBean deleteUser(String id) {

        return iUserService.delete(id);
    }

    /**
     * 更新用户
     *
     * @author lw
     * @data 2021/07/07 上午
     */
    @UserLoginToken
    @ApiOperation(value = "更新", notes = "更新功能")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public RespBean updateUser(User user) {
        return iUserService.updateUser(user);
    }


    /**
     * 测试注解sql(select)
     */
    @ApiOperation(value = "sql", notes = "sql")
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ResponseBody
    public RespBean getMessage1() {
        return RespBean.success("success", userVoService.sqlQueryAll());
    }


    /**
     * 测试注解sql(update)
     */
    @ApiOperation(value = "sql", notes = "sql")
    @RequestMapping(value = "/updateSql", method = RequestMethod.GET)
    @ResponseBody
    public RespBean uodateSql(String userId) {
        iUserService.sqlDelete(userId);
        return RespBean.success("删除成功",userVoService.queryAll());


    }


    /**
     * mybatis逻辑删除,使用mybatis自己封装的方法实现了逻辑删除
     */
    @ApiOperation(value = "sql", notes = "sql")
    @RequestMapping(value = "/logicDelSql", method = RequestMethod.GET)
    @ResponseBody
    public RespBean logicDelSql(String id) {
        iUserService.removeById(id);
        return RespBean.success("success", userVoService.sqlQueryAll());


    }
}
